package br.com.mmm.javacenter.framework.view.taglib.helper;

import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;

import br.com.mmm.javacenter.framework.view.ContextAttributes;

public class Form extends Element{
	private String nome;
	private String controller;
	private String action;
	private Object entity;
	private String legenda;
	private String hiddens;
	private boolean forwardParameters;

	public Form(JspContext context, JspFragment fragment) {
		super(context, fragment);
	}	
	
	public void make() throws JspException, IOException {
		
		Writer writer = getJspContext().getOut();
		writer.write("<form ");
		writer.write(getAtributo("name",nome));
		writer.write(getAtributo("method","post"));
		writer.write(getAtributo("action",getContextPath("/"+controller+"/"+action+".auto")));
		writer.write(" >");
		
		if(hiddens!=null){
			String[] split = hiddens.split(",");
			for(String s:split){
				writer.write("<input ");
				writer.write(getAtributo("type", "hidden"));
				writer.write(getAtributo("name", s));
				writer.write(getAtributo("id", s));
				writer.write(getAtributo("value", getAttribute(s)));
				writer.write("/>");
			}
		}
		if(forwardParameters){
			Enumeration parameterNames = getRequest().getParameterNames();
			while(parameterNames.hasMoreElements()){
				String nextElement = (String) parameterNames.nextElement();
				writer.write("<input ");
				writer.write(getAtributo("type", "hidden"));
				writer.write(getAtributo("name", nextElement));
				writer.write(getAtributo("id", nextElement));
				writer.write(getAtributo("value", getRequest().getParameter(nextElement)));
				writer.write("/>");
			}
		}
		if(legenda!=null){
			writer.write("<fieldset>");
			writer.write("<legend>");
			writer.write(legenda);
			writer.write("</legend>");
		}
		writer.write("<TABLE class='tableForm' cellpadding=0 cellspacing=0 border=1 123>");
		writer.write("<tr>");
		writer.write("<td " +
				getAtributo("id", nome + "_msg") +
				getAtributo("class", "mensagem") + 
				">");
		List<Alerta> alertas = (List<Alerta>) getRequest().getAttribute(ContextAttributes.REQUEST_ALERTAS);
		for(Alerta alerta:alertas){
			writer.write("<div ");
			writer.write(getAtributo("class", "alerta"+alerta.getSeveridade()));
			writer.write("/>");
			writer.write(alerta.getMensagem());
			writer.write("</div>");
		}
		writer.write("</td>");
		writer.write("</tr>");
		writeBody(writer);
		writer.write("</TABLE>");
		if(legenda!=null){
			writer.write("</fieldset>");
		}
		writer.write("</form>");
	}


	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	public String getController() {
		return controller;
	}
	public void setController(String controller) {
		this.controller = controller;
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	public Object getEntity() {
		return entity;
	}
	public void setEntity(Object entity) {
		this.entity = entity;
	}


	public String getLegenda() {
		return legenda;
	}


	public void setLegenda(String legenda) {
		this.legenda = legenda;
	}

	public String getHiddens() {
		return hiddens;
	}

	public void setHiddens(String hiddens) {
		this.hiddens = hiddens;
	}

	public boolean isForwardParameters() {
		return forwardParameters;
	}

	public void setForwardParameters(boolean forwardParameters) {
		this.forwardParameters = forwardParameters;
	}	

}
